<div class="ajaxLoading">
<img src='<?php echo $engineReturn['admin_template']?>css/images/loading.gif' width="50" align="middle" />
</div>
<div class='ajaxGrid'>
<?php
	if(isset($engineReturn['record']['msg']) && is_array($engineReturn['record']['msg']))
		foreach($engineReturn['record']['msg'] as $msg)
		{
			echo "<div class='inlineMessage'>$msg</div>";
		}
	$countField = 0;
	foreach($engineReturn['record']['fieldSetting'] as $i=>$v)
	{
		if($v['view'])
			$countField++;
	}
	if($engineReturn['edit'] && $engineReturn['delete'] && $engineReturn['sub_input'])
		$cp = 5;
	else if($engineReturn['edit'] && $engineReturn['delete'])
		$cp = 4;
	else if($engineReturn['edit'] && $engineReturn['sub_input'])
		$cp = 4;
	else if($engineReturn['delete'] && $engineReturn['sub_input'])
		$cp = 4;
	elseif($engineReturn['edit'] || $engineReturn['delete'] || $engineReturn['sub_input'])
		$cp = 3;
	else
		$cp = 2;
	$total		= $engineReturn['record']['recTotal'];
	$startOri	= $engineReturn['record']['recStart'];
	$count		= $engineReturn['record']['recPage'];
	$start		= $startOri/$count;
	$start++;
	$pageCount 	= ceil($total/$count);
	$step		= 4;
	$nav[0]		= 1;
	$res	= $engineReturn['record']['records'];
		for($i = 2; $i <= $pageCount; $i++)
		{
			if(($i==$start)
			|| ($i<$step)
			|| (($i>$start-$step) & ($i<$start))
			|| (($i<$start+$step) & ($i>$start))
			|| ($i>$pageCount-$step)
			)
			{
				$nav[count($nav)] = $i;
			}
		}
		echo "<table class='table'>";
		echo "<tr><td>";
		foreach($nav as $i=>$n)
		{
			if(($i>0) && ($nav[$i]-$nav[$i-1]>1))
				echo "..";
			echo "<a href='#' class='aNav navigasi_grid ".(($n==$start)?"aNavdisabled":"")."'>".$n."</a>";
		}
		echo " Rows";
		echo "<select id='rowGrid'>";
			echo "<option value=''>--</option>";
			echo "<option value='5' ".(($count==5)?"selected='selected'":"").">5</option>";
			echo "<option value='10' ".(($count==10)?"selected='selected'":"").">10</option>";
			echo "<option value='20' ".(($count==20)?"selected='selected'":"").">20</option>";
			echo "<option value='50' ".(($count==50)?"selected='selected'":"").">50</option>";
			echo "<option value='100' ".(($count==100)?"selected='selected'":"").">100</option>";
		echo "</select>";
		echo "<a href='#' class='aNav navigasi_grid'>Refresh</a>";
		echo "<a href='#' class='aNav'>Print</a>";
		echo "<a href='#' class='aNav'>Excel</a>";
		echo "</td></tr>";
		echo "</table>";
	if(count($res)>0)
	{
		echo "<table class='table'>";
		echo "<tr>";
			echo "<th rowspan='2' colspan='".$cp."'>";
			echo "</th>";
			foreach($engineReturn['record']['fieldSetting'] as $i=>$v)
			{
				if($v['view'])
				{
					switch($v['input'])
					{
						case "medium_table":
						case "advance_table":
							$rs = count($res[0][$v['name']][1]);
							echo "<th colspan='".$rs."'>";
								echo $v['text'];
							echo "</th>";
						break;
						default:
							echo "<th rowspan='2'>";
								echo $v['text'];
							echo "</th>";
						break;
					}
				}
			}
		echo "</tr>";
		echo "<tr>";
			foreach($engineReturn['record']['fieldSetting'] as $i=>$v)
			{
				if($v['view'])
				{
					switch($v['input'])
					{
						case "medium_table":
						case "advance_table":
							foreach($res[0][$v['name']][1] as $fv)
							{
								echo "<th>";
									echo $fv;
								echo "</th>";
							}
						break;
						default:
						break;
					}
				}
			}
		echo "</tr>";
		for($i = 0; $i<count($res); $i++)
		{
			$id	= $res[$i][$engineReturn['record']['fieldSetting'][0]['name']];
			echo "<tr>";
				echo "<td>";
				echo "<a href='#' class='btnInfo' idrec='$id' title='Press Here to Get Detail'>";
					echo "<img src='".$engineReturn['admin_template']."css/images/info.png' />";
				echo "</a>";
				echo "<div class='containerAjax'></div>";
				echo "</td>";
				if($engineReturn['sub_input'])
				{
					echo "<td>";
					echo "<a idrec='$id' title='Press Here to Input Sub Data' href='".$baseUrlSave.$otority."/engine/".$engineReturn['modulename']."/normal/sub/all/nav/".$res[$i][$engineReturn['record']['fieldSetting'][0]['name']]."'>";
						echo "<img src='".$engineReturn['admin_template']."css/images/sub.png' />";
					echo "</a>";
					echo "</td>";
				}
				if($engineReturn['edit'])
				{
					echo "<td>";
					echo "<a idrec='$id' title='Press Here to Edit Data' href='".$baseUrlSave.$otority."/engine/".$engineReturn['modulename']."/normal/insert/".$res[$i][$engineReturn['record']['fieldSetting'][0]['name']]."'>";
						echo "<img src='".$engineReturn['admin_template']."css/images/edit.png' />";
					echo "</a>";
					echo "</td>";
				}
				if($engineReturn['delete'])
				{
					echo "<td>";
					echo "<a href='#' idrec='$id' title='Press Here to Delete Data' class='delete_data_on_grid'>";
						echo "<img src='".$engineReturn['admin_template']."css/images/del.png' />";
					echo "</a>";
					echo "</td>";
				}
				echo "<td>".($startOri+$i+1)."</td>";
				foreach($engineReturn['record']['fieldSetting'] as $v)
				{
					$fieldName	= $v['name'];
					if($v['view'])
					{
						switch($v['input'])
						{
							case "text":
							case "text_area":
								echo "<td class='tdGrid'>";
									if($v['edit']==1)
									{
										echo "<div class='columnGrid'>";
										echo "<a idrecord='$id' fieldname='$fieldName' href='#' class='editIcon'>";
										echo "<img src='".$engineReturn['admin_template']."css/images/edit.png' />";
											echo "</a>";
											echo "</div>";
												echo "<div>";
													echo $res[$i][$fieldName];
												echo "</div>";
									}
									else
										echo $res[$i][$fieldName];
							break;
							case "hidden":
							case "text_primary":
							case "password":
							case "text_area_plus_editor":
								echo "<td>";
									echo $res[$i][$fieldName];
							break;
							case "checkbox":
								echo "<td>";
								if(is_array($res[$i][$fieldName]))
								{
									foreach($res[$i][$fieldName] as $v)
									{
										echo $v.", ";
									}
								}
							break;
							case "image":
								echo "<td>";
								if($res[$i][$fieldName]!="")
									echo "<div class='defContainerImage'>"
										."<img src='".$res[$i][$fieldName]."'>"
										."</div>";
							break;
							case "file":
								echo "<td>";
								if($res[$i][$fieldName]!="")
									echo "<a href='".$res[$i][$fieldName]."' target='_SELF'>"
										."DOWNLOAD</a>";
							break;
							case "radio":
							case "select":
								echo "<td class='tdGrid'>";
									if($v['edit']==1)
									{
										echo "<div class='columnGrid'>";
										echo "<a idvalue='".$res[$i][$fieldName]."' idrecord='$id' fieldname='$fieldName' href='#' class='editIcon'>";
										echo "<img src='".$engineReturn['admin_template']."css/images/edit.png' />";
											echo "</a>";
											echo "</div>";
												echo "<div>";
													if(isset($res[$i][$fieldName][1]))
														echo $res[$i][$fieldName][1];
													else
														echo $res[$i][$fieldName][0];
												echo "</div>";
									}
									else
									{
													if(isset($res[$i][$fieldName][1]))
														echo $res[$i][$fieldName][1];
													else
														echo $res[$i][$fieldName][0];
									}
							break;
							case "simple_table":
								echo "<td class='tdGrid'>";
									if($v['edit']==1)
									{
										echo "<div class='columnGrid'>";
										echo "<a idrecord='$id' fieldname='$fieldName' href='#' class='editIcon'>";
										echo "<img src='".$engineReturn['admin_template']."css/images/edit.png' />";
											echo "</a>";
											echo "</div>";
												echo "<div>";
													if(isset($res[$i][$fieldName]['dataRec']))
														echo $res[$i][$fieldName]['dataRec'];
													else
														echo "-";
												echo "</div>";
									}
									else
									{
										if(isset($res[$i][$fieldName]['dataRec']))
											echo $res[$i][$fieldName]['dataRec'];
										else
											echo "-";
									}
							break;
							case "medium_table":
							case "advance_table":
	//							echo "<pre>";
	//							print_r($res[$i][$fieldName]);
								$rs = count($res[$i][$fieldName][1]);
	//							echo "<td colspan='$rs'>";
									if(isset($res[$i][$fieldName][0][0]) && (is_array($res[$i][$fieldName][0][0])))
									{
	//									echo "<table class='table'>";
	//										echo "<tr>";
											foreach($res[$i][$fieldName][0][0] as $t)
											{
												echo "<td>".$t."</td>";
											}
	//										echo "</tr>";
	//									echo "</table>";
									}
									else
									{
										echo "<td colspan='$rs'>";
									}
							break;
						}
						echo "</td>";
					}
				}
			echo "</tr>";
		}
	echo "</table>";
	}
	?>
</div>
<script type="text/javascript">
	$('.ajaxGrid').delay(100).slideDown('slow', function(){
			$('.ajaxLoading').slideUp('slow');
	});
	
	$('.navigasi_grid').navigateViewData({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/view/data/',
		div_loading 	: 'ajaxLoading',
		div_grid		: 'ajaxGrid',
		div_container 	: 'containerGrid',
		count			: '<?php echo $count?>',
		start			: '',
		field_filter	: {
			<?php
			$num = 0;
			if(is_array($engineReturn['record']['filterField']))
			foreach($engineReturn['record']['filterField'] as $key=>$row)
			{
				echo $num." : {0:'".$row['name']."', 1:'".$engineReturn['record']['filterField'][$key]['input']."'}, \n";
				$num++;
			}
			?>
			},
		len_filter	: '<?php
			if(is_array($engineReturn['record']['filterField']))
				echo count($engineReturn['record']['filterField']);
			else
				echo 0;
			?>'
		});

	$('#rowGrid').navigateViewData({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/view/data/',
		div_loading 	: 'ajaxLoading',
		div_grid		: 'ajaxGrid',
		div_container 	: 'containerGrid',
		count			: '',
		start			: '<?php echo $start?>',
		changeRow		: true,
		field_filter	: {
			<?php
			$num = 0;
			if(is_array($engineReturn['record']['filterField']))
			foreach($engineReturn['record']['filterField'] as $key=>$row)
			{
				echo $num." : {0:'".$row['name']."', 1:'".$engineReturn['record']['filterField'][$key]['input']."'}, \n";
				$num++;
			}
			?>
			},
		len_filter	: '<?php
			if(is_array($engineReturn['record']['filterField']))
				echo count($engineReturn['record']['filterField']);
			else
				echo 0;
			?>'
		});

		
	$('.btnInfo').navigateDetailData({
		url	: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/detail_view/',
		});
	$('.delete_data_on_grid').deleteData({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/view/data/',
		div_loading 	: 'ajaxLoading',
		div_grid		: 'ajaxGrid',
		div_container 	: 'containerGrid',
		count			: '<?php echo $count?>',
		start			: '<?php echo $start?>'
		});


	$('.tdGrid').hover(function(){
			$(this).find('.columnGrid').show('slow', function(){
				$(this).find('.editIcon').slideDown('medium');
				});
		}, function(){
			$(this).find('.editIcon').slideUp('medium', function(){
				$(this).parent().hide('slow');
			});
		});
	$('.editIcon').navigateToEditDataNormal({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/single_edit/',
		});

</script>